package com.eric.server.utils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * Created by eric on 2017/11/17.
 */
public class SystemUtils {
    public static String getLocalHost() {
        try{
            Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (allNetInterfaces.hasMoreElements())
            {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                System.out.println(netInterface.getName());
                Enumeration addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements())
                {
                    ip = (InetAddress) addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address)
                    {
                        if(!ip.getHostAddress().startsWith("127"))
                            return ip.getHostAddress();
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return "";

    }
}
